package com.kc.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.JSONResult;
import com.kc.sys.common.R;
import com.kc.sys.domain.Loginfo;
import com.kc.sys.service.LoginfoService;
import com.kc.sys.vo.LogInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-10-17
 */
@RestController
@RequestMapping("/loginfo")
public class LoginfoController {

    @Autowired
    private LoginfoService loginfoService;

    /**
     * 全查询
     */
    @RequestMapping("loadAllLoginfo")
    public DataGridView loadAllLoginfo(LogInfoVo logInfoVo){

        Page<Loginfo> page = new Page<>(logInfoVo.getPage(), logInfoVo.getLimit());
        QueryWrapper<Loginfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginname()),"loginname",logInfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginip()), "loginip",logInfoVo.getLoginip());
        queryWrapper.ge(logInfoVo.getStartTime()!=null,"logintime",logInfoVo.getStartTime());
        queryWrapper.le(logInfoVo.getEndTime()!=null,"logintime",logInfoVo.getEndTime());
        queryWrapper.orderByDesc("logintime");
        this.loginfoService.page(page,queryWrapper);
        return  new DataGridView(page.getTotal(), page.getRecords());
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteLogin")
    public R deleteById(Integer id){
        try {
            loginfoService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    /**
     * 批量删除
     * @param logInfoVo
     * @return
     */
    @RequestMapping("deleteLogins")
    public R deleteByIds(LogInfoVo logInfoVo){
        try {
            ArrayList<Serializable> ids = new ArrayList<>();
            for (Integer id : logInfoVo.getIds()) {
                ids.add(id);
            }
            loginfoService.removeByIds(ids);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }
}

